package com.example.DownloadImageHelperLibrary;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class DownloadImageTask extends AsyncTask<String,Integer,Void> {

    ImageView imageView;
    ProgressBar progressBar;
    TextView loadingImgText;
    File path;
    String name;

    long size=0;
    long current_size=0;

    public DownloadImageTask(ImageView img, ProgressBar progressBar, TextView textView, File folder, String name){
        this.imageView = img;
        this.path = folder;
        this.name = name;

        this.progressBar = progressBar;
        this.progressBar.setMax(100);
        this.progressBar.setVisibility(View.VISIBLE);

        this.loadingImgText = textView;
        this.loadingImgText.setVisibility(View.VISIBLE);
    }
    
    //---------------
    public DownloadImageTask(ImageView imageView, ProgressBar progressBar, File folder, String name){
        this.imageView = imageView;
        this.path = folder;
        this.name = name;
        this.progressBar = progressBar;
        this.progressBar.setMax(100);
        this.progressBar.setVisibility(View.VISIBLE);
      
    }

    public DownloadImageTask(ImageView imageView, File folder, String name){
        this.imageView = imageView;
        this.path = folder;
        this.name = name;

    }

    public DownloadImageTask(File folder, String name){
        this.path = folder;
        this.name = name;

    }

    public void download(String url){
        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                return;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                path = new File(this.path, name);
                FileOutputStream fileOutputStream = new FileOutputStream(path);
                try {
                    size= entity.getContentLength();
                    inputStream = entity.getContent();
                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;

                    while ((bufferLength = inputStream.read(buffer)) > 0){
                        fileOutputStream.write(buffer,0,bufferLength);
                        current_size+=bufferLength;
                        double p = (double)current_size*100/size;
                        Log.i("Downloading "+name,": "+ p);
                        publishProgress((int) p);

                    }
                    fileOutputStream.close();
                    return;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            getRequest.abort();
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

    @Override
    protected Void doInBackground(String... params) {
        //android.os.Debug.waitForDebugger();
        download(params[0]);
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        if(progressBar!=null&&progressBar.isIndeterminate()==false)
        {
            progressBar.setProgress(values[0]);
            if(loadingImgText!=null){
                loadingImgText.setText(values[0]+"%");
            }
        } 
        //android.os.Debug.waitForDebugger();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        String s = path.getPath();
        try{
            Bitmap bitmap = BitmapFactory.decodeFile(s);
            if(bitmap!=null){
                if(imageView!=null)
                    imageView.setImageBitmap(bitmap);
                if(progressBar!=null)
                    progressBar.setVisibility(View.GONE);
                if(loadingImgText!=null)
                    loadingImgText.setVisibility(View.GONE);
            }
        }
        catch (Exception e){
            Log.e("Bitmap", "Error bit map");

        }


    }
}
